capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local log "";
local crime "";
local output "";


/* 
Prep 4, collapse data
*/


local fs_vars  "age18_yes n_age18unfav n_onssi1922 n_onssi1922_exact n_onssi20 " ;
local hh_vars "n_earnhh9702 n_earnhh9712 n_emphhpost9702 n_emphhpost9712 n_inchh9702 n_inchh9712" ;
local covariates "n_male n_firstage n_fam_singmom n_fam_nopar n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_diag1_mental n_diag1_nerv n_diag1_sense n_diag1_infec n_diag1_endo n_diag1_none n_diag1_cong n_diag1_musc n_diag1_resp n_diag1_blood n_diag1_neo n_earnhhpre9095" ;
local main_vars "n_crime* c_crime* n_yr* n_1* n_2* n_3* n_4* avg*" ;
local demo_list "all male female " ; 
local controls_all "n_male i.n_firstage i.n_diag1cat n_dibmdr_mie n_dibmdr_mine n_dibmdr_miss n_fam_singmom n_fam_nopar i.n_earnhhbins i.state" ; 

foreach demo in `demo_list' { ;

	use "`crime'\prep2\crime_prwora_prepforreg.dta", clear ;

	/* Subsamples */
	gen demo_all = 1 ;
	gen byte demo_othment	= (n_diag1_mental == 1 & (substr(dibdig,1,3) != "317" & substr(dibdig,1,3) != "318" & substr(dibdig,1,3) != "319" )) ;
	gen byte demo_nonment 	= (demo_othment == 0) ;
	gen byte demo_intel 	= (n_diag1_mental == 1 & (substr(dibdig,1,3) == "317" | substr(dibdig,1,3) == "318" | substr(dibdig,1,3) == "319" )) ;
	gen byte demo_phys 		= (n_diag1_mental != 1) ;
	gen byte demo_twopar 	= (n_fam_singmom != 1 & n_fam_nopar != 1) ;
	gen byte demo_singno 	= (n_fam_singmom == 1) | (n_fam_nopar ==1) ;
	gen byte demo_nopar  	= (n_fam_nopar == 1) ;
	gen byte demo_male 	 	= (n_male ==1) ;
	gen byte demo_female 	= (n_male !=1) ;
	gen byte demo_white		= (race == "W") ;
	gen byte demo_black 	= (race == "B") ;
	gen byte demo_nonw		= (race != "W") ;

	rename cnty_urate cnty_ue ;
	rename cnty_crimerate cnty_crime ;
	rename cnty_share_blue cnty_dem ;
	rename cnty_pop2000 cnty_pop ;

	foreach cnty in ue crime dem pop { ;
		summ cnty_`cnty', det ;
		gen byte demo_`cnty'high = (cnty_`cnty' > r(p50) & cnty_`cnty' != .) ;
		gen byte demo_`cnty'low = (cnty_`cnty' <= r(p50) & cnty_`cnty' != .) ;
		replace demo_`cnty'high = . if cnty_`cnty' == . ;
		replace demo_`cnty'low = . if cnty_`cnty' == . ;
		tab demo_`cnty'high, m ;
		tab demo_`cnty'low, m ;
	} ;

	summ n_earnhhpre9095, det ;
	replace n_earnhhpre9095 = 0 if n_earnhhpre9095 == . ;
	gen byte demo_parhigh = (n_earnhhpre9095 > r(p50) & n_earnhhpre9095 != .) ;
	gen byte demo_parlow = (n_earnhhpre9095 <= r(p50) & n_earnhhpre9095 != .) ;
	replace demo_parhigh = . if n_earnhhpre9095 == . ;
	replace demo_parlow = . if n_earnhhpre9095 == . ;
	tab demo_parhigh, m ;
	tab demo_parlow, m ;

	keep if demo_`demo' == 1 ;

	/* Replace covariates and FS vars with missing if not in main sample */
	foreach var of varlist 
		 `covariates' `fs_vars' `hh_vars'
		{ ;
			replace `var' = `var' if keep_1997_2017_any == 1 ;
		} ;

	summ dobwk_run ;
	local dobwk_max = r(max) ;
	local dobwk_min = r(min)*-1 ;
	gen dob2wk_run = . ;
	forval wk = 0(2)`dobwk_max' { ;
		replace dob2wk_run = (`wk'+`=`wk'+1')/2 if dobwk_run == `wk' | dobwk_run == `=`wk'+1' ;
	} ;
	forval wk = 1(2)`dobwk_min' { ;
		replace dob2wk_run = -(`wk'+`=`wk'+1')/2 if dobwk_run == -`wk' | dobwk_run == -`=`wk'+1' ;
	} ;
	tab dobwk_run, m ;
	tab dob2wk_run, m ;
	gen dob2wk_stata = dobwk_stata ;

	foreach time in wk /*2wk*/ mo { ;

		preserve;
		
		foreach var of varlist 
			`main_vars' `fs_vars' `hh_vars'
			{ ;
			xi: reg `var' `controls_all' ;
			predict r_`var', r ;
			} ;
		
		foreach var of varlist 
			dobdd_run dob`time'_stata 
			`main_vars' `covariates' `fs_vars' `hh_vars'
			r_*
			{ ;
			gen N`var' = `var' ;
			} ;

		
		collapse	(mean)	
							dobdd_run dob`time'_stata
							`main_vars' `covariates' `fs_vars' `hh_vars'
							r_*
					(count) N* , 
			by(dob`time'_run) ;
			
		foreach var of varlist 
			dobdd_run dob`time'_stata 
			`main_vars' `covariates' `fs_vars' `hh_vars'
			r_*
			{ ;
			replace `var' = . if N`var' < 10 ;
			} ;
		
		rename Ndob`time'_stata count ;
			
		save "`output'\prwora_crime_collapse`time'_`demo'.dta", replace ;	
		restore ;
	} ; /* end time */
	
} ; /* end demo */

capture log close;
